package com.wang.common.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.wang.common.pojo.ApiResponse;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.RegexUtil;
import com.wang.common.util.VerifyCodeUtils;
import com.wang.users.service.IUsersDbService;
import com.wang.users.service.impl.UsersDbServiceImpl;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

/**
 * 放的是所有公共的代码
 *
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/CommonServlet")
public class CommonServlet extends BaseServlet {
    /* 工具类 */
    private RegexUtil regexUtil = new RegexUtil();

    private IUsersDbService usersDbService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
        this.usersDbService = usersDbServiceImpl;
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("==CommonServlet==service=");
        String method = request.getParameter("method");
        if ("code".equalsIgnoreCase(method)) {
            /* 验证码 */
            this.code(request, response);
        } else if ("uploadFile".equalsIgnoreCase(method)) {
            /* 打开上传文件 */
            this.uploadFile(request, response);
        } else if ("uploadFileSubmit".equalsIgnoreCase(method)) {
            /* 上传文件提交 */
            this.uploadFileSubmit(request, response);
        } else if ("uploadFileBack".equalsIgnoreCase(method)) {
            /* 打开上传文件 */
            this.uploadFileBack(request, response);
        } else if ("uploadFileBackSubmit".equalsIgnoreCase(method)) {
            /* 上传文件提交 */
            this.uploadFileBackSubmit(request, response);
        }
    }

    /**
     * 生成一个验证码
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void code(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        // 生成随机字串
        /* String verifyCode = VerifyCodeUtil.generateVerifyCode(4); */
        /* 使用自己的类,生成验证码 */
        String verifyCode = this.regexUtil.randStr(4);
        // 存入会话session
        HttpSession session = request.getSession(true);
        /* 将图片中的验证码放到了session中 */
        session.setAttribute("randSess", verifyCode);
        // 生成图片
        int w = 100, h = 40;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
    }

    /**
     * 专门用来处理上传图片
     *
     * @return
     * @throws ServletException
     */
    public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/uploadFile.jsp";
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        String fileType = "未知类型";
        String operType = request.getParameter("operType");
        if ("users".equalsIgnoreCase(operType)) {
            /* 用户上传头像 */
            fileType = "用户头像";
        }
        if (updateFlag == null || "".equalsIgnoreCase(updateFlag)) {
            /* 清除信息 */
            apiResponse.clearInfo();
        }
        request.setAttribute("fileType", fileType);
        request.setAttribute("response", apiResponse.toJSON());
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 专门用来处理上传图片
     *
     * @return
     */
    public void uploadFileSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        String fileFileName = "";
        File file = null;

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("fileName", fileFileName);
        paramsMap.put("souFile", file);

        /* 搜索条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        /* 相关id  */
        String relaId = request.getParameter("relaId");
        /* 类型 */
        String operType = request.getParameter("operType");
        if ("goods".equalsIgnoreCase(operType)) {


        }
        ConstatFinalUtil.SYS_LOGGER.info("上传照片;relaId:{},结果:{}", relaId, apiResponse.toJSON().toJSONString());
        /* 返回结果 */
        this.printOut(apiResponse.toJSON().toJSONString(), response);
    }

    /**
     * 专门用来处理上传图片
     *
     * @return
     * @throws ServletException
     */
    public void uploadFileBack(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/uploadFileBack.jsp";
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        String fileType = "未知类型";
        String operType = request.getParameter("operType");
        if ("users".equalsIgnoreCase(operType)) {
            /* 用户上传头像 */
            fileType = "用户头像";
        } else if ("board".equalsIgnoreCase(operType)) {
            /* 用户上传头像 */
            fileType = "板块图片";
        }
        if (updateFlag == null || "".equalsIgnoreCase(updateFlag)) {
            /* 清除信息 */
            apiResponse.clearInfo();
        }
        request.setAttribute("fileType", fileType);
        request.setAttribute("response", apiResponse.toJSON());
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }

    /**
     * 专门用来处理上传图片
     *
     * @return
     */
    public void uploadFileBackSubmit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 文件上传 */
        Map<String, Object> fileUploadMap = this.fileUploadUtil(request);

        String fileName = fileUploadMap.get("fileName") + "";
        InputStream is = (InputStream) fileUploadMap.get("is");

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("fileName", fileName);
        paramsMap.put("is", is);

        /* 搜索条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        /* 相关id  */
        String relaId = request.getParameter("relaId");
        /* 类型 */
        String operType = request.getParameter("operType");
        if ("goods".equalsIgnoreCase(operType)) {


        }
        ConstatFinalUtil.SYS_LOGGER.info("上传照片;relaId:{},结果:{}", relaId, apiResponse.toJSON().toJSONString());
        /* 返回结果 */
        this.printOut(apiResponse.toJSON().toJSONString(), response);
    }
}
